//
//  XMLUtil.m
//  backpack
//
//  Created by wujian on 16/9/3.
//  Copyright © 2016年 wujian. All rights reserved.
//

#import "XMLUtil.h"

@implementation XMLUtil

-(instancetype)init{

    self = [super init];
    
    if (self) {
        
        //初始化数组，存放解析后的数据
        self.list = [NSMutableArray array];
    }
    
    return self;

}

//几个代理方法的实现，是按逻辑上的顺序排列的，但实际调用过程中中间三个可能因为循环等问题乱掉顺序
//开始解析
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    
    NSLog(@"parserDidStartDocument...");
    
}

//准备节点
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict{
    
    NSLog(@"%@",self.currentElement);
    
    self.currentElement = elementName;
    
    if ([self.currentElement isEqualToString:@"trkpt"]) {
        
       [self.list addObject:attributeDict];
    }
}

//获取节点内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    

}

- (void)parserDidEndDocument:(NSXMLParser *)parser{
    
    NSLog(@"parserDidEndDocument...");
    
    if ([self.delegate respondsToSelector:@selector(parseComplete:)]) {
        
        [self.delegate parseComplete:self.list];
    }
}

-(void)parse:(NSString *)path{
    
    
//    NSFileManager* fm = [NSFileManager defaultManager];
    
//    NSData* data = [[NSData alloc] init];
    
//    data = [fm contentsAtPath:path];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    
    self.par = [[NSXMLParser alloc]initWithData:data];
    
    //添加代理
    self.par.delegate = self;
    
    [self.par parse];
    
}

@end
